package com.boe.csb.core.service;

import com.boe.csb.core.common.utils.SendEmailUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * Created by tianxiang.luo on 16/12/27.
 */
@Service
public class SendMailService {

    private Logger logger = LoggerFactory.getLogger(SendMailService.class);

    private ThreadPoolExecutor mailSendExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(10);

    public void asyncSendMail(String revEmail,String subject,String content){
        mailSendExecutor.execute(new TaskThread(revEmail,subject,content));
    }

    private class TaskThread implements Runnable{
        public TaskThread(String revEmail, String subject, String content) {
            this.revEmail = revEmail;
            this.subject = subject;
            this.content = content;
        }
        private String revEmail;
        private String subject;
        private String content;
        @Override
        public void run() {
            try {
                SendEmailUtil.sendMail(revEmail,subject,content);
            }catch (Exception e){
                logger.error("邮件发送失败:接受人{}主题{}内容{}",revEmail,subject,content);
            }
        }
    }
}
